package ua.amsoft.brelok;

import ua.amsoft.brelok.constant.Constants;
import ua.amsoft.brelok.preference.BrelokPreferenceActivity;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

/**
 * @author Tair Mustafaiev
 */
public class MainActivity extends FragmentActivity {
    
    private ViewPager viewPager;
    
    private final ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        @Override
        public void onTabReselected(final Tab tab, final FragmentTransaction ft) {
        }
        
        @Override
        public void onTabSelected(final Tab tab, final FragmentTransaction ft) {
            MainActivity.this.viewPager.setCurrentItem(tab.getPosition(), true);
        }
        
        @Override
        public void onTabUnselected(final Tab tab, final FragmentTransaction ft) {
        }
    };
    
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        this.viewPager = (ViewPager) this.findViewById(R.id.brelok_pager_view);
        final BrelokFragmentStatePagerAdapter adapter = new BrelokFragmentStatePagerAdapter(
                this.getSupportFragmentManager());
        this.viewPager.setAdapter(adapter);
        final ActionBar actionBar = this.buildActionBarTabs();
        
        final TextView signature = (TextView) this.findViewById(R.id.app_signature);
        signature.setText(Constants.getAppSignature(this.getApplicationContext()));

        this.viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(final int position) {
                // When swiping between pages, select the
                // corresponding tab.
                actionBar.setSelectedNavigationItem(position);
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        this.getMenuInflater().inflate(R.menu.activity_statistics, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        final Bundle extras = this.getIntent().getExtras();
        if (extras != null) {
            final Integer widgetId = (Integer) extras.get(AppWidgetManager.EXTRA_APPWIDGET_ID);
            final Intent intent = new Intent(this, BrelokPreferenceActivity.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
            this.startActivityForResult(intent, Activity.RESULT_OK);
            return true;
        }
        return false;
    }
    
    /**
     * Builds the ActionBar tabs
     * 
     * @return {@link ActionBar} object with added tabs
     */
    private ActionBar buildActionBarTabs() {
        final ActionBar actionBar = this.getActionBar();
        // Specify that a dropdown list should be displayed in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        // create new Statistics tab
        this.addTab(actionBar, R.string.title_activity_statistics);
        // create new Assignments tab
        this.addTab(actionBar, R.string.title_activity_assignments);
        // create new Inbox assignments tab
        this.addTab(actionBar, R.string.title_activity_in);
        // create new Outbox assignments tab
        this.addTab(actionBar, R.string.title_activity_out);
        // create new Reports tab
        this.addTab(actionBar, R.string.title_activity_reports);
        return actionBar;
    }
    
    /**
     * Adds tab to action bar
     * 
     * @param actionBar - {@link ActionBar}
     * @param title - tab's title
     */
    private void addTab(final ActionBar actionBar, final int title) {
        final Tab statisticsTab = actionBar.newTab().setText(this.getApplicationContext().getString(title))
                .setTabListener(this.tabListener);
        actionBar.addTab(statisticsTab);
    }
}
